package portal.action.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import portal.beans.Gpu;
import portal.common.ResultData;
import portal.conf.entity.GPUEntity;
import portal.dao.ScheduleGpuDetailDao;
import portal.mq.MQProducer;

/************************************************************************************
 * @author 徐文远
 * @since  2018-08-01
 * Action 遵循 RestFul 操作规范
 * GET  查询
 * POST 新增/功能操作
 * PUT  全量更新
 * PATCH 部分更新
 * DELETE 删除
 * **********************************************************************************/
@RestController 
public class GPUAction extends BaseAction{
	@Autowired
	ScheduleGpuDetailDao scheduleGpuDetailDao;
	//private static ApplicationContext context =null;//new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext.xml","classpath:spring-mybatis.xml"});
   //private static HostService hostService=(HostService) context.getBean("hostService");
	/*static{
		try{
			checkGpu();
			loadHosts();
		}catch(Exception e){
			//e.printStackTrace();
		}
	}*/
	/*****
	 * 添加 GPU 信息
	 * 1、更新配置文件
	 * 2、更新数据库
	 * ****/
	@RequestMapping(value="/gpuInfo",method=RequestMethod.POST)
	public ResultData addGpuEntity(GPUEntity gpu){
		if(elimGPU.insertConf(gpu))
			MQProducer.sendMessage(elimGPU.getFileName());
		else{
			return error("更新到配置文件失败");
		}
		//System.out.println(gpu.toString());
		
		Gpu gpu1 = new Gpu();
		
		Gpu find=scheduleGpuDetailDao.getGpuByVersion(gpu.getKey());
		if(find !=null && find.getGpuVersion()!=null)
			return success("该型号CPU 已存在无需重复录入！");
		gpu1.setGpuIdentify(gpu.getRes());
		gpu1.setGpuVersion(gpu.getName());
		gpu1.setGpuDetail(gpu.getInfo());
		boolean addFlag = scheduleGpuDetailDao.addGpu(gpu1);
      if(addFlag==false)
    	   return error("配置文件已更新，未正确录入到数据库");
		return success("操作成功");
		//return (GPUEntity)elimGPU.findConfByName(gpu.getRes());
		//return null;
	}

}
